package com.prim.threadcoreknowledge.threadobjectclasscommonmethods;

/**
 * 手写join的代替洗发
 */
public class JoinPrinciple {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "执行完毕");
            }
        });
        thread.start();
        System.out.println("开始等待子线程运行完毕");
        //主线程等待子线程执行完毕后再继续执行
//        thread.join();
        //等价于join 主线程拿到锁
        synchronized (thread){
            //执行wait 主线程陷入休眠状态notify唤醒主线程
            thread.wait();
        }
        //线程执行完毕会自动调用notifyAll JVM层实现的
        System.out.println("所有子线程执行完毕");
    }
}
